#include "junix.h"
using namespace std;

char* word_end(char* s, char *e) {
	char *p=s;
	while (*p!=' ' && p!=e) {
		++p;
	}

	return p;
}

char *word_beg(char *s,  char *e) {
	char *p=s;
	while (!isalnum(*p) && p!=e) {
		++p;
	}	

	return p;
}

int main(int argc, char **argv)
{
	typedef tr1::unordered_map<string,int> MAP;
	MAP dic;

	FILE *fp=fopen("BIGTXT","r");
	if (!fp) {
		perror("fopen");
		exit(EXIT_FAILURE);
	}

	const size_t sz = 1024;
	char buf[sz];
	clock_t beg = clock();
	char *b,*e,*wb,*we;

	while ( fgets(buf,sz,fp) != NULL) {
		b=buf;
		e=buf+strlen(buf);
		if (*(e-1)=='\n') {
			*--e = '\0';
		}

		while (true) {
			wb=word_beg(b,e);
			b = wb;
			we= word_end(b,e);
			b = we;
			if (wb==we) break;
			string s(wb,we-wb);
			++ dic[s];
		}
	}

	fclose(fp);

	cout<<"Time:"<<clock()-beg<<endl;
	cout<<"Size:"<<dic.size()<<endl;
}
